// pages/checkCoupon/checkCoupon.js
const app = getApp()
var request = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    windowHeight: 0,
    scrollViewHeight: 0,
    couponList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let parkId = options.parkId;
    let type = options.type;
    // 先取出页面高度 windowHeight
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
    let scrollViewHeight = that.data.windowHeight - that.data.statusBarHeight - 44 - 27;
    that.setData({
      scrollViewHeight: scrollViewHeight,
      parkId: parkId,
      type: type
    })
    that.getCouponList(parkId, type);
  },
  /**
   * 获取停车券列表
   * @param {String} parkId 停车场ID
   * @param {String} type 停车券类型
   */
  getCouponList(parkId, type) {
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/cMyticket/getNUseDSCTickets?type=" + type + "&userId=" + userId + "&parkId=" + parkId;
    request.http(url, "POST")
      .then((res) => {
        if (res.data.code == 200) {
          let couponList = res.data.data;
          if (couponList != null && couponList.length > 0) {
            for (let i = 0; i < couponList.length; i++) {
              couponList[i].check = false;
              couponList[i].beginTime = couponList[i].beginTime.substring(0, 10);
              couponList[i].endTime = couponList[i].endTime.substring(0, 10);
            }
            that.setData({
              couponList: couponList
            })
          }
        }
      }).catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 选中优惠券
   * @param {*} event 
   */
  checkItem(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let couponList = that.data.couponList;
    let ticketCode = couponList[index].id;
    couponList[index].check = true;
    that.setData({
      couponList: couponList
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      ticketCode: ticketCode
    })
    wx.navigateBack({
      delta: 1,
    })
  }
})